package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.pojo.MenuTvVo;

import java.util.List;

public class MenuTvContentAdapter extends ArrayAdapter<MenuTvVo> {


    private List<MenuTvVo> items;
    private Context mcontext;

    public MenuTvContentAdapter(@NonNull Context context, @NonNull List<MenuTvVo> items) {
        super(context, 0);
        this.items = items;
        this.mcontext = context;
    }

    public void resetAll(List<MenuTvVo> items) {

        if (items == null || items.size() < 1) {
            return;
        }

        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();

    }

    public void addItems(List<MenuTvVo> items) {

        if (items == null || items.size() < 1) {
            return;
        }
        this.items.addAll(items);
        notifyDataSetChanged();

    }


    public void addItem(MenuTvVo news) {

        if (news == null || news.getTitle().length() < 1) {
            return;
        }
        this.items.add(news);
        notifyDataSetChanged();

    }

    public void clearItem() {
        items.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return this.items.size();
    }

    @Nullable
    @Override
    public MenuTvVo getItem(int position) {

        if (position < 0 || position >= items.size()) {
            return null;
        }

        return this.items.get(position);
    }

    public List<MenuTvVo> getItems() {
        return items;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position >= items.size()) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    private View getCreateView(ViewGroup parent) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
        view = layoutInflater.inflate(R.layout.item_menu_tv_content, parent, false);
        return view;
    }

    class ViewHolder {
        TextView name, type, url, time;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        MenuTvVo news = getItem(position);
        if (convertView == null) {
            convertView = getCreateView(parent);
            ViewHolder holder = new ViewHolder();
            if (convertView != null) {

                holder.name = convertView.findViewById(R.id.name);
                holder.type = convertView.findViewById(R.id.type);
                holder.url = convertView.findViewById(R.id.url);
                holder.time = convertView.findViewById(R.id.time);

                convertView.setTag(holder);

            }
        }

        if (convertView != null) {
            final ViewHolder holder = (ViewHolder) convertView.getTag();

            if (holder.name != null) {

                holder.name.setText(news.getName());

            }

            if (holder.url != null) {
//                holder.url.setText(news.getUrl());
                holder.url.setText(news.getTitle());
            }

            if (holder.type != null) {
                holder.type.setText(news.getType());
            }

            if (holder.time != null) {
                holder.time.setText(news.getTime());
            }

        }


        return convertView;
    }
}
